const { ccclass, property } = cc._decorator;
declare let i18n,DBManager,dbh;
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");
import PopUIHelper = require("../../utils/PopUIHelper");
import Utils = require("../../utils/Utils");
//恢复心跳 关卡6
@ccclass
export default class DeputyRecoverHeartView extends BaseView {
    @property(cc.Label)
    lblStep: cc.Label = null;
    @property(cc.Label)
    lblDes: cc.Label = null;

    @property(cc.Node)
    bgNode: cc.Node = null;

    @property(cc.Node)
    drawArea: cc.Node = null;

    @property(sp.Skeleton)
    momSpine:sp.Skeleton = null;

    @property(sp.Skeleton)
    girlSpine:sp.Skeleton = null;

    @property(sp.Skeleton)
    heartSpine:sp.Skeleton = null;

    @property(cc.Graphics)
    graphics: cc.Graphics = null;

    @property(cc.Node)
    point1:cc.Node = null;

    @property(cc.Node)
    point2:cc.Node = null;

    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;

    @property(cc.Node)
    rightLine:cc.Node = null;

    _done = false;
    _shanShuo = false;
    _isTcouh1 = false;
    _isTcouh2 = false;
    _isOut = false;
    _isBegin = false;
    _wrongTimes = 0;
    _high = 230;
    _low = 170;
    _isHigh = false;
    _isLow = false;
    _lastLoc: cc.Vec2;
    _drawLineDis = 5;
    _isStart = false;
    onLoad() {
        Utils.audioManager.playBGM("huazhuang/deputyGame/deputyGameBg");
        this.bgNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.bgNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.bgNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
        this.lblStep.string = i18n.t("DEPUTY_GAME_STEP_TITLE6");
        this.lblDes.string = i18n.t("DEPUTY_GAME_STEP_DES6");
        Utils.audioManager.playSound("huazhuang/deputyGame/step_5_cry");
    };
    onClose(){
        Utils.audioManager.playDefaultBGM();
    };
    onTouchStart(event){
        if(this._shanShuo)
            return;
        if(this._done)
            return;
        this._isBegin = true;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.bgNode.parent.convertToNodeSpaceAR(worldPoint);
        this._lastLoc = localPoint;
        this.graphics.moveTo(localPoint.x, localPoint.y);
        this._isTcouh1 = this.point1.getBoundingBoxToWorld().contains(worldPoint);
    };

    onTouchMove(event){
        if(!this._isBegin)
            return;
        if(this._shanShuo)
            return;
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.bgNode.parent.convertToNodeSpaceAR(worldPoint);
        if (this._lastLoc) {
            let _dis = cc.Vec2.distance(localPoint, this._lastLoc);
            if (_dis > this._drawLineDis) {
                this.graphics.moveTo(this._lastLoc.x, this._lastLoc.y);
                this.graphics.lineTo(localPoint.x, localPoint.y);
                // this.graphics.circle(localPoint.x, localPoint.y, 5)
                // this.graphics.fill();
                this.graphics.stroke();
                this._lastLoc = localPoint;
            }
        }
        if(!this.drawArea.getBoundingBoxToWorld().contains(worldPoint)){//是否出框
            this._isOut = true;
        }
        if(localPoint.y > this._high){//高度峰值
            this._isHigh = true;
        }
        if(localPoint.y < this._low){//最低峰值
            this._isLow = true;
        }
    };
    onTouchEnd(event){
        if(!this._isBegin)
            return;
        if(this._shanShuo)
            return;
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        this._isTcouh2 = this.point2.getBoundingBoxToWorld().contains(worldPoint);
        if(this._isHigh && this._isLow && this._isTcouh1 && this._isTcouh2 && !this._isOut){
            this._done = true;
            this.graphics.clear();
            this.rightLine.active = false;
            Utils.audioManager.playSound("huazhuang/deputyGame/step_5_success");
            this.heartSpine.setAnimation(0,"correct",false);
            this.girlSpine.setAnimation(0,"correct",false);
            this.momSpine.setAnimation(0,"correct",false);
            this.momSpine.setCompleteListener(()=>{
                this.endNode.active = true;
                Utils.audioManager.playSound("huazhuang/deputyGame/pass");
            });
        }else{
            this._shanShuo = true;
            this.graphics.node.opacity = 0;
            this.bgNode.runAction(cc.sequence(
                cc.delayTime(0.3), cc.callFunc(()=> {
                    this.graphics.node.opacity = 255;
            }), cc.delayTime(0.3), cc.callFunc(()=> {
                this.graphics.node.opacity = 0;
            }), cc.delayTime(0.3), cc.callFunc(()=> {
                this.graphics.node.opacity = 255;
            }), cc.delayTime(0.3), cc.callFunc(()=> {
                this._shanShuo = false;
                this._isOut  = false;
                this._isBegin = false;
                this._isHigh = false;
                this._isLow = false;
                this.graphics.clear();
                this._wrongTimes ++;
                this.rightLine.active = this._wrongTimes >= 5;
            })))
        }
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };
    
    onClickSkip(){
        this.onClickEnd(true);
    };

    //提示
    onClickTips(){
        PopUIHelper.helper.showNewConfirmMsg(i18n.t("DEPUTY_GAME_TIPS6"));
    };
    
    
}
